<template>
    <div>


    </div>
</template>

<script>
import {defineComponent} from 'vue'
import ValidCode from "@/conponents/ValidCode.vue";

export default defineComponent({
    name: "MyFavorites",
    components: {ValidCode},
    data() {
        return {
            dialogFormVisible_edit: false,      // 编辑按钮弹出框
            dialogVisible_delete: false,        // 删除按钮弹出框
            dialogVisible_deleteSelect: false,  // 选中删除按钮弹出框
            dialogVisible_addProduct: false,      // 添加商品按钮弹出框

            account: {},
            userId: '',

            favoriteId: '',             // 存储被删除商品的id
            ids: [],                 // 存储被选中商品的id
            favorites: [],              // 存储商品信息

            currentPage: 1,          // 当前页码
            pageSizes: [5, 10, 50, 100], // 每页显示条数
            pageSize: 5,             // 每页显示条数
            total: 0,                // 商品总数
        }
    },

    mounted() {
        // 获取用户ID
        this.account = JSON.parse(localStorage.getItem('account'))
        this.userId = this.account.id
        this.loadFavorites()
    },

    methods: {
        loadFavorites() {
            this.$request.post('/userFavoriteProduct/getFavoritesByUserId' + '?currentPage=' + this.currentPage
                + '&pageSize=' + this.pageSize + '&userId=' + this.userId).then(res => {
                if (res.code === '200') {
                    this.favorites = res.data.list
                    this.total = res.data.total
                } else {
                    this.$message.error(res.msg)
                }
            })
        },
        handleOpen(key, keyPath) {
            console.log(key, keyPath);
        },
        handleClose(key, keyPath) {
            console.log(key, keyPath);
        },
        handleSelect(key, keyPath) {
            this.$router.push(key)
            this.activeIndex = key
        },
        toMain() {
            this.$router.push('/main')
        },

        // 删除选中二次确认
        handleDeleteFavorites() {
            this.dialogVisible_deleteSelect = false;
            if (this.ids.length > 0) {
                // 删除选中
                this.$request.post('/userFavoriteProduct/deleteByIds', this.ids).then(res => {
                    if (res.code === '200') {
                        // 刷新数据
                        this.loadFavorites()
                    } else {
                        this.$message.error(res.msg);
                    }
                })
            } else {
                this.$message.info('请选择要删除的商品');
            }
        },

        // 删除选中二次提醒
        deleteSelectFavorites() {
            // 显示二次提醒弹窗
            this.dialogVisible_deleteSelect = true;
        },

        // 获取选中的商品id，
        // 并存储到 ids 数组中
        handleSelectCodeForTicket(val) {
            this.ids = val.map((item) => item.id);
        },

        // currentPage 改变时会触发
        handleCurrentChange(val) {
            // 获取  当前页 和 页面大小  currentPage、pageSize
            this.currentPage = val;

            // 发送请求获取相应数量的数据
            this.loadFavorites()
        },

        //  pageSize 改变时会触发
        handleSizeChange(val) {
            // 获取  当前页 和 页面大小  currentPage、pageSize
            this.pageSize = val;
            this.currentPage = 1;

            // 发送请求获取相应数量的数据
            this.loadFavorites()
        },

        // 删除按钮二次提醒
        handleDeleteButton(product) {
            this.favoriteId = product.id
            this.handleDeleteProduct()
        },

        // 删除当前商品
        handleDeleteProduct() {
            this.$request.delete('/userFavoriteProduct/deleteById/' + this.favoriteId).then(res => {
                if (res.code === '200') {
                    this.favoriteId = ''
                    // 重新加载数据
                    this.loadFavorites()
                } else {
                    this.$message.error(res.msg);
                }
            })
        },
    }
})
</script>


<style scoped>

.el-dialog__body {
    padding: 10px 30px;
}


</style>